package com.ericsson.enap.modules.procLog.service.impl;

import com.ericsson.common.pojo.FTPInfoVO;
import com.ericsson.common.util.DateUtils;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.enap.modules.procLog.constant.ProcConstant;
import com.ericsson.enap.modules.procLog.mapper.ProcLogMapper;
import com.ericsson.enap.modules.procLog.service.ImsBossKpiService;
import com.ericsson.enap.modules.procLog.thread.ImsBossKpiThread;
import com.ericsson.enap.modules.sys.entity.EnipNetelement;
import com.ericsson.enap.modules.sys.entity.EpcDict;
import com.ericsson.enap.modules.sys.service.EpcDictService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @description: bossKpi功能service实现类
 * @author: yanghai
 * @create: 2021-05-25 09:55
 */
@Lazy(true)
@Service
public class ImsBossKpiServiceImpl implements ImsBossKpiService {

    private Logger log = LoggerUtils.Logger(LogFileName.ImsBossKpiJob);

    @Autowired
    private ProcLogMapper procLogMapper;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private EpcDictService epcDictService;

    @Override
    public void dealBossKpiFile(String collectorIp, String imsBossKpiPath, Date now, String delay) {
        //1.查询当前采集器配置了哪些网元需要进行下载
        log.info("======= bizType:"+9+" ======= collectorIp:"+collectorIp);
        List<EnipNetelement> list = procLogMapper.queryNetelementList(9,collectorIp);
        //2.多线程处理下载的网元
        FTPInfoVO fTPInfoVO;
        for(int i = 0;i<list.size();i++){
            fTPInfoVO = getFtpInfoVO(list, i);
            String localPath = imsBossKpiPath+ File.separator + list.get(i).getId() + File.separator;
            ImsBossKpiThread imsBossKpiThread = new ImsBossKpiThread(list.get(i).getId(),fTPInfoVO,localPath,now,log,mongoTemplate,delay);
            Thread t = new Thread(imsBossKpiThread);
            t.start();
        }
        log.info("===== All EDA boss kpi analysis files end! =====");
    }

    private FTPInfoVO getFtpInfoVO(List<EnipNetelement> list, int i) {
        FTPInfoVO fTPInfoVO = new FTPInfoVO();
        fTPInfoVO.setHost(list.get(i).getIp4());
        fTPInfoVO.setPort(21);
        fTPInfoVO.setUserName(list.get(i).getUsername());
        fTPInfoVO.setPassword(list.get(i).getPassword());
        fTPInfoVO.setNetElementID(list.get(i).getId());
        fTPInfoVO.setPath(list.get(i).getDownload_path());
        return fTPInfoVO;
    }

    public void deleteMongodbData(Date now) {
        EpcDict epcDict = new EpcDict();
        epcDict.setKeyName(ProcConstant.ProcLogDays);
        epcDict = epcDictService.selectEpcDict(epcDict);
        if (null != epcDict && !"".equals(epcDict.getKeyValue())) {
            String deleteTime = DateUtils.getStatisTime(Integer.parseInt(epcDict.getKeyValue()) * 1440 + 30, now, "yyyy-MM-dd HH:mm:00");
            log.info("deleteTime-------------------------------:" + deleteTime);
            Query query = Query.query(Criteria.where("dateTime").lte(deleteTime));
            mongoTemplate.remove(query, "imsboss_kpi");
        }
    }

}
